﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DalatCooking.Core;
using DalatCooking.Core.Helper;
public partial class UserControls_Cart : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        Page.Title = "Khóa học đang chọn";
        
        if (!IsPostBack)
        {
            ShowCart();
            if (Session.GetShoppingCart() != null)
            {
                rptCart.DataSource = Session.GetShoppingCart().ShoppingCart;
                rptCart.DataBind();
            }
            if (RemoveID != -1)
            {
                RemoveCart();
            }
        }       
    }
    void ShowCart()
    {
        Course c = CourseManager.FindById(CourseID);
        Cart ShoppingCart;
        if (Session.GetShoppingCart() != null)
            ShoppingCart = Session.GetShoppingCart();
        else
            ShoppingCart = new Cart();

        if (c != null)
        {
            Cart cart = new Cart { CourseId = c.CourseId, Picture = c.Picture, Name = c.Name, Price = c.Price, Quantity = 1 };
            ShoppingCart.AddToCart(cart);

            Session.SetShoppingCart(ShoppingCart);
            Response.Redirect("/viewcart");
        }        
    }
    void RemoveCart()
    {
        Cart c = Session.GetShoppingCart();
        if (c != null)
        {
            c.RemoveCart(RemoveID);
            Response.Redirect("/viewcart");
        }
    }
    public int RemoveID
    {
        get
        {
            if (Request.QueryString["CourseID"] != null)
                return ConvertObject.ToInt(Server.HtmlEncode(Request.QueryString["CourseID"]));
            return -1;
        }
    }
    public int CourseID
    {
        get
        {
            if (Request.QueryString["ID"] != null)
                return ConvertObject.ToInt(Server.HtmlEncode(Request.QueryString["ID"]));
            return -1;
        }
    }
    public float Totals
    {
        get
        {
            if (Session.GetShoppingCart() != null)
            {
                return Session.GetShoppingCart().Totals();
            }
            else return 0;
        }
    }
    public float Tax
    {
        get
        {
            return 10;
        }
    }
    public float TotalCheckout
    {
        get
        { return Totals + Tax; }
    }
    protected void btnCheckout_Click(object sender, EventArgs e)
    {
        Cart shoppingCart = Session.GetShoppingCart(); 
        Person p = Session.GetPersonLogged();
        if (shoppingCart != null && p != null)
        {
            Orders order = new Orders { OrderID = -1, PersonID = p.PersonId, Tax = 10, Totals = TotalCheckout, Notes = "Test thu coi sao" };
            shoppingCart.Checkout(order);

            // Send email
            Session.RemoveShoppingCart();
            Response.Redirect("/complete");
        }
        else
        {
            Response.Redirect("/account");
        }
    }
}